package com.lotter.api.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lotter.common.exception.LotterCusException;
import com.lotter.common.util.DateUtil;
import com.lotter.dao.OnlineUserMapper;
import com.lotter.invocation.StaticDataContext;
import com.lotter.model.user.OnlineUser;

@Service
public class OnlineUserService {
	
	@Autowired
	private OnlineUserMapper onlineUserMapper;
	
	@Transactional(rollbackFor=Exception.class)
	public void initOnlingUser() throws LotterCusException{
		onlineUserMapper.delete(new Date());
		List<OnlineUser> list = onlineUserMapper.findOnlineUser(new Date());
		StaticDataContext.init(list);
	}
	
	
	@Transactional(rollbackFor=Exception.class)
	public void updateOnlineUser(Long uid) throws LotterCusException{
		onlineUserMapper.updateLoginTimeout(uid, DateUtil.getDateplusDay(new Date(), 60, null, null, null));
	}
	
	@Transactional(rollbackFor=Exception.class)
	public void logout(Long uid) throws LotterCusException{
		onlineUserMapper.deleteByUid(uid);
	}


}
